package com.example.origin.finance.service.impl;

import cn.hutool.json.JSONUtil;
import com.example.origin.finance.dto.XueqiuQuote;
import com.example.origin.finance.service.AbstractStoreService;
import com.example.origin.resttemplate.util.RestUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class XueqiuQuoteStoreService extends AbstractStoreService<XueqiuQuote> {
    @Override
    protected XueqiuQuote getData(String code) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("extend", "detail");
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Cookie", getXueqiuCookie());
        String u1 = "https://stock.xueqiu.com/v5/stock/quote.json";

        if (!setParamMap(code, paramMap)) {
            return null;
        }
        ResponseEntity<Object> response = RestUtil.restGet(restTemplate, u1, paramMap, headerMap, ParameterizedTypeReference.forType(Map.class));
        return JSONUtil.parseObj(response).getJSONObject("body").getJSONObject("data").get("quote", XueqiuQuote.class);
    }
}
